#!/bin/sh

# Slackware build script for arptables
# Written by Menno E. Duursma
# Modified by the SlackBuilds.org project

PRGNAM=arptables
VERSION=0.0.3
ARCH=${ARCH:-i386}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=`pwd`		
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP || exit 1
rm -rf $PRGNAM-v0.0.3-2
tar xzvf $CWD/$PRGNAM-v0.0.3-2.tar.gz || exit 1
cd $PRGNAM-v0.0.3-2 || exit 1

chown -R root:root .
chmod -R a-s,u+w,go+r-w .

mkdir -p $PKG/usr/{man,sbin}

# Don't set uid and gid of installed files in the Makefile 
# This breaks stuff for anyone who might want to test the
# script as a normal user.  We'll take care of ownership
# and permissions in the build script
cat $CWD/Makefile.diff | patch -p1 || exit 1

make || exit 1
make install PREFIX=$PKG/usr || exit 1

( cd $PKG
  find . -type f \
    | xargs file \
    | grep "executable" \
    | grep ELF | cut -f 1 -d : \
    | xargs strip --strip-unneeded \
  2> /dev/null
  find . -type f | xargs file \
    | grep "shared object" \
    | grep ELF \
    | cut -f 1 -d : \
    | xargs strip --strip-unneeded \
  2> /dev/null
)

if [ -d $PKG/usr/man ]; then
  gzip -9 $PKG/usr/man/man?/*
fi

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/README > $PKG/usr/doc/$PRGNAM-$VERSION/README
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz

# Clean up the extra stuff
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$PRGNAM-$VERSION
  rm -rf $PKG
fi

